package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BrandServiceImpl implements BrandService {
        @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public List<Integer> findByid(Integer id) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("admin_id",id);

        List list = brandMapper.selectList(wrapper);
        List<Integer> list1 = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            DtsBrand dtsBrand = (DtsBrand)list.get(i);
            list1.add(dtsBrand.getId());

        }
        return list1;
    }

    @Override
    public List<Map<String, Object>> selectAll() {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("deleted",false);

        List list = brandMapper.selectList(wrapper);

        List<Map<String,Object>> list1 = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            DtsBrand brand = (DtsBrand) list.get(i);
            Map<String,Object> map = new HashMap<>();
            map.put("value",brand.getId());
            map.put("label",brand.getName());
            list1.add(map);

        }
        return list1;
    }

    @Override
    public DtsBrand findById(Integer brandId) {


        DtsBrand dtsBrand = brandMapper.selectById(brandId);
        return dtsBrand;
    }

    @Override
    public List<Integer> findByAdminId(Integer id) {
         QueryWrapper wrapper = new QueryWrapper();
         wrapper.eq("admin_id",id);

        List list = brandMapper.selectList(wrapper);
        List<Integer> brandsId = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            DtsBrand brand = (DtsBrand)list.get(i);

            brandsId.add(brand.getId());
        }
        return brandsId;

    }

    @Override
    public Map<String, Object> getPage(Integer page, Integer limit, String sort, String order, List<Integer> brandId, String brandName,Integer Id) {
         Map<String,Object> map = new HashMap<>();

        IPage iPage = new Page(page,limit);
        QueryWrapper wrapper = new QueryWrapper();
        if("desc".equalsIgnoreCase((order))){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }
        if(brandName!=null){
            wrapper.eq("name",brandName);
        }
        if(Id!=null){
            wrapper.eq("id",Id);
        }else {
            if(brandId!=null && brandId.size()>0){
                wrapper.in("id",brandId);
            }
        }

        brandMapper.selectPage(iPage,wrapper);

        System.out.println(iPage.getRecords());
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public void create(DtsBrand dtsBrand) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("deleted",false);

        brandMapper.insert(dtsBrand);
    }

    @Override
    public void delete(DtsBrand brand) {
        int id = brand.getId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        brandMapper.deleteById(brand.getId());

    }

    @Override
    public void update(DtsBrand dtsBrand) {

        brandMapper.updateById(dtsBrand);
    }


}
